import base64
import os
import uuid

from pymongo import MongoClient

from func import Func

ip = "192.168.1.234"
port = 80
user_name = 'admin'
user_password = 'yitutech123'
func_client = Func(ip, port, user_name, user_password)

mongo_host = '192.168.1.12'
mongo_port = 27017

card_path = r"D:\school\card"
stranger_path = r"D:\school\passer"


def get_face_feature(repo_id, hezi_face_id):
    face_result = func_client.get_face_one(repo_id, hezi_face_id)
    if face_result.get("status") == 200:
        feature = face_result.get("message")
        return feature
    else:
        return ""


client = MongoClient(host=mongo_host, port=int(mongo_port))
card_ = client.tttt.card
passer_ = client.tttt.passer


def get_passer():
    print(func_client.create_repo(1))
    print(func_client.create_repo(2))

    index = 10000
    for img_file in os.listdir(stranger_path):
        # 添加一个异常
        index += 1
        print(index)
        if index < 200000:
            repo_id = 1
        if index >= 200000:
            if index == 200000:
                func_client.delete_repo(1)
            repo_id = 2

        img_path = os.path.join(stranger_path, img_file)
        url_path = '192.168.1.12:17777/passer/' + img_file
        with open(img_path, "rb+") as f:
            face_bytes = f.read()
            face_base64 = str(base64.b64encode(face_bytes), "utf-8")
            face_id = str(uuid.uuid4())
            result = func_client.insert_image(repo_id, index, face_base64, face_id)
            if result.get("message") != "success":
                continue
            feature = get_face_feature(repo_id, index)
            if feature:
                dic = {
                    '_id': face_id,
                    'hezi_face_id': index,
                    'feature': feature,
                    'face_base64': face_base64,
                    'picture_uri': url_path
                }
                print(dic)
                passer_.insert_one(dic)


def get_card():
    print(func_client.create_repo(3))

    index = 0
    for img_file in os.listdir(card_path):
        # 添加一个异常
        index += 1
        print(index)
        repo_id = 3
        img_path = os.path.join(card_path, img_file)
        url_path = '192.168.1.12:17777/card/' + img_file
        img_sp = img_file.split('__')[1].split('_')
        person_id = img_sp[1]
        name = img_sp[2]

        with open(img_path, "rb+") as f:
            face_bytes = f.read()
            face_base64 = str(base64.b64encode(face_bytes), "utf-8")
            face_id = str(uuid.uuid4())
            result = func_client.insert_image(repo_id, index, face_base64, face_id)
            if result.get("message") != "success":
                continue
            feature = get_face_feature(repo_id, index)
            if feature:
                dic = {
                    '_id': face_id,
                    'hezi_face_id': index,
                    'feature': feature,
                    'face_base64': face_base64,
                    'picture_uri': url_path,
                    'person_id': person_id,
                    'name': name
                }
                print(dic)

                card_.insert_one(dic)


get_card()
